package com.wangjie;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangjie.mapper.UserMapper;
import com.wangjie.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Map;

/**
 * @author jieshao
 * @date 2022/2/10 16:49
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class)
public class UserTest02 {
    @Autowired
    private UserMapper userMapper;

    @Test
    public void test01() {
        Page<User> page = new Page<>(1, 2);
        // 条件构造器
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.isNull("name").ge("age", 20).isNotNull("email");

        Page<User> userPage = userMapper.selectPage(page, wrapper);
        List<User> list = userPage.getRecords();
        list.forEach(System.out::println);
    }

    @Test
    public void test02() {
        // 条件构造器
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.between("age", 20, 26);
        List<User> list = userMapper.selectList(wrapper);
        list.forEach(System.out::println);
    }

    @Test
    public void test03() {
        // 条件构造器
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.select("name", "email").like("email", "test").likeRight("name", "张");
        List<Map<String, Object>> map = userMapper.selectMaps(wrapper);
        map.forEach(System.out::println);
    }

    @Test
    public void test04() {
        // 条件构造器
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("age", "id");
        List<User> list = userMapper.selectList(wrapper);
        list.forEach(System.out::println);
    }
}